package com.stone.upms.upms.service.impl;

import com.stone.upms.upms.dao.ApplicationSystemRepository;
import com.stone.upms.upms.domain.ApplicationSystem;
import com.stone.upms.upms.service.ApplicationSystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class ApplicationSystemServiceImpl implements ApplicationSystemService {

    @Autowired
    private ApplicationSystemRepository repository;

    @Override
    public void saveObj(ApplicationSystem applicationSystem) {
        repository.saveAndFlush(applicationSystem);
    }

    @Override
    public ApplicationSystem getObjById(Long id) {
        Optional<ApplicationSystem> tempResult = repository.findById(id);
        if (tempResult.isPresent()) {
            return tempResult.get();
        }
        return null;
    }


    @Override
    public Page<ApplicationSystem> findAll(ApplicationSystem applicationSystem, Integer pageNumber, Integer pageSize) {
        pageNumber = ((null == pageNumber) ? 0 : (pageNumber - 1));
        pageSize = ((null == pageSize) ? 10 : pageSize);
        Pageable pageable = PageRequest.of(pageNumber, pageSize);
        return repository.findAll(getWhereClause(applicationSystem), pageable);
    }

    @Override
    public void deleteObj(Long id) {
        repository.deleteById(id);
    }


    /**
     * 动态查询条件 where从句
     */
    private Specification<ApplicationSystem> getWhereClause(ApplicationSystem applicationSystem) {
        return (Specification<ApplicationSystem>) (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicate = new ArrayList<>();
            if (applicationSystem != null) {
                if (null != applicationSystem.getId() && applicationSystem.getId() != 0) {
                    predicate.add(criteriaBuilder.equal(root.get("id").as(Long.class), applicationSystem.getId()));
                }
                if (null != applicationSystem.getName() && !"".equals(applicationSystem.getName().trim())) {
                    predicate.add(criteriaBuilder.like(root.get("name").as(String.class), "%" + applicationSystem.getName().trim() + "%"));
                }
                if (null != applicationSystem.getOnOff() && !"".equals(applicationSystem.getOnOff().trim())) {
                    predicate.add(criteriaBuilder.equal(root.get("onOff").as(String.class), applicationSystem.getOnOff().trim()));
                }
                if (null != applicationSystem.getUrl() && !"".equals(applicationSystem.getUrl().trim())) {
                    predicate.add(criteriaBuilder.equal(root.get("url").as(String.class), applicationSystem.getUrl().trim()));
                }
            }
            Predicate[] pre = new Predicate[predicate.size()];
            return criteriaQuery.where(predicate.toArray(pre)).getRestriction();
        };
    }
}
